package com.quanshu.shop.goods.view;

import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.goods.adapter.SearchTxtAdapter;
import com.quanshu.shop.main.api.ShopMainIF;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchActivity extends BaseActivity {

    @BindView(R.id.base_title_bar_left_img)
    ImageView baseTitleBarLeftImg;
    @BindView(R.id.base_title_bar_left_rl)
    RelativeLayout baseTitleBarLeftRl;
    @BindView(R.id.search_edit)
    EditText searchEdit;
    @BindView(R.id.base_title_bar_right_txt)
    TextView baseTitleBarRightTxt;
    @BindView(R.id.base_title_bar_right_rl)
    RelativeLayout baseTitleBarRightRl;
    @BindView(R.id.search_title)
    ConstraintLayout searchTitle;
    @BindView(R.id.search_txt)
    TextView searchTxt;
    @BindView(R.id.search_rl)
    RecyclerView searchRl;
    private List<String> stringList;
    private SearchTxtAdapter searchTxtAdapter;
    public static final String SEARCH_TXT = "search_body";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initView() {
        ImmersionBar.with(this).titleBar(searchTitle).statusBarColor(R.color.white).statusBarDarkFont(true).init();
        RxViewHelper.getInstance().clicks(baseTitleBarLeftRl, aVoid -> {
            finish();
        });
        RxViewHelper.getInstance().clicks(baseTitleBarRightRl, aVoid -> {
            //搜索
            if (!TextUtils.isEmpty(searchEdit.getText())) {
                goSearchGoodsActivity(searchEdit.getText().toString());
            }
        });
        FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(this);
        flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP);
        flexboxLayoutManager.setFlexDirection(FlexDirection.ROW);
        flexboxLayoutManager.setAlignItems(AlignItems.STRETCH);
        searchRl.setLayoutManager(flexboxLayoutManager);
        getSearchTxt();
    }

    //初始化大家都在搜内容
    private void getSearchTxt() {
        NetClient.getInstance()
                .getShopService(ShopMainIF.class)
                .getSearchTxtInfo("")
                .compose(new BaseTransformer<>(this)).subscribe(new BaseObserver<BaseResponse<List<String>>>() {
            @Override
            public void onSuccess(BaseResponse<List<String>> response) {
                stringList = response.getData();
                searchTxtAdapter = new SearchTxtAdapter(stringList);
                searchTxtAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                        switch (view.getId()) {
                            case R.id.item_goods_property_child_txt:
                                goSearchGoodsActivity(stringList.get(position));
                                break;
                        }
                    }
                });
                searchRl.setAdapter(searchTxtAdapter);
            }
        });
    }

    private void goSearchGoodsActivity(String body) {
        Bundle bundle = new Bundle();
        bundle.putString(SEARCH_TXT, body);
        BaseTool.goActivity(this, SearchGoodsListActivity.class, bundle);
    }
}
